home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Software Vault: The Gold Collection
/
Software Vault - The Gold Collection (American Databankers) (1993).ISO
/
cdr53
/
pctv4n_1.zip
/
QEDICT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-06-10
|
8KB
|
384 lines
/**************************************************
* FILE NAME: QEdict.c TITLE: dictionary listing
*
* AUTHOR: K. E. North II, Resource Group, Inc.
*
* SYNOPSIS:
*
* List record structure info for Q+E Lib
*
***************************************************
*
* initialize (strings, counts, error_status)
* assign variables to table, view
*
* initiate data base connection -
* dbConnect() to login, get cursor
* execute SQL
* dbRequest() - prep/exec SQL statement
*
* IF status <> 0 then
* while <> end of data:
* get dictionary info (dbDescribe)
* format and list columns
* ELSE
* process system error message
* ENDIF
* terminate data base connection: dbClose()
*
***************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
#ifndef __QEDEFS_H
#include "QEdefs.h"
#endif
#ifndef __QEVARBL_H
#include "QEvarbl.h"
#endif
#ifndef __QEFUNCS_H
#include "qefuncs.h"
#endif
#ifndef __DBTYPES_H
#include "dbtypes.h"
#endif
#ifndef __REQUEST_H
#include "request.h"
#endif
#ifndef __RQOPTION_H
#include "rqoption.h"
#endif
#ifndef __QCOLINFO_H
#include "qcolinfo.h"
#endif
#ifndef __DATASRC_H
#include "datasrc.h"
#endif
#include "qedict.h"
#undef DEBUG
#undef DEBUG1
#define RCOUNT 10
#define DISPLAYLEN 20
int ExitStatus;
int StatusReturned;
static char DBDictPath[80];
static char DBTable[32];
static char DBDriver[9];
char *table;
char *dictpath;
char *driver;
COLUMNS ColInfo[MAXFIELD];
COLUMNS *fi;
struct ConnectionInfo *si;
struct ViewInfo *vu;
struct DataSource *dsrc;
struct SQLRequest *rq;
struct RequestOptions *op;
void main (int argc, char *argv[])
{
HSTMT hstmt;
POINTER statement;
int nColumns;
int ColPosition;
int ColCount;
int flen; /* string length of field name */
int i; /* blank fill loop index */
int TotalLength; /* sum of field lengths */
static char SQLString[MAXSQL];
static char DictColName[DISPLAYLEN+1];
char *ColDataType;
/* allocate memory */
op = calloc(1, sizeof(struct RequestOptions));
si = calloc(1, sizeof(struct ConnectionInfo));
rq = calloc(1, sizeof(struct SQLRequest));
vu = calloc(1, sizeof(struct ViewInfo));
dsrc = calloc(1, sizeof(struct DataSource));
/* initialize strings */
memset(SQLString,'\x0',sizeof(SQLString));
memset(DBDictPath,'\x0',sizeof(DBDictPath));
memset(DBTable,'\x0',sizeof(DBTable));
memset(DBDriver,'\x0',sizeof(DBDriver));
table = &DBTable[0];
dictpath = &DBDictPath[0];
driver = &DBDriver[0];
StatusReturned = SQL_SUCCESS;
GetArgs(argc,argv[1],argv[2],argv[3],
table,dictpath,driver);
/* make selection of database type */
strcpy (dsrc->DBDriver, driver);
/* if (strcmp,QEPDX)
{
strcpy (dsrc->DBDriver, "QEPDX");
}
if (strcmp,QEDBF)
*/
/* for DBF and PDX, same dict and datapath */
strcpy (dsrc->DBDictPath, dictpath);
strcpy (dsrc->DBDataPath, dictpath);
op->UseDirectExec = TRUE;
op->RequiresCursor = FALSE;
op->UseTransaction = FALSE;
rq->CursorID = 0; /* no cursor used for QEDBF */
/*******************************************/
/* open data base - connect to data source */
/*******************************************/
strcpy (si->DataPath, dsrc->DBDictPath);
strcpy (si->DictPath, dsrc->DBDictPath);
/* may require revision for named cursors */
StatusReturned = dbConnect (si, dsrc);
if ((StatusReturned != SQL_SUCCESS))
{
printf
("\n<QEDict> ERROR %d: Unable to connect to %s",
si->hdbc, dsrc->DBDriver);
exit (1);
}
/* specify query string */
strcpy(SQLString,"SELECT * FROM ");
strcat(SQLString,dsrc->DBDataPath);
SQLString[strlen(SQLString)] = '\x5c';
strcat(SQLString,DBTable);
/************************/
/* prepare and execute */
/************************/
StatusReturned = SQL_SUCCESS;
/* may require revision for named cursors */
StatusReturned = dbRequest( rq, SQLString, si);
if ((StatusReturned != SQL_SUCCESS))
{
if (StatusReturned < 0 )
{
printf
("\n<QEdict> STATUS: preparing %s\n",
SQLString);
}
else
{
printf
("\n<QEdict> ERROR: preparing %s\n",
SQLString);
exit (1);
}
}
/* Loop to get the description for each */
/* column of the table. Set the loop count */
/* from the number of columns for this */
/* request's SQL statement */
nColumns = FindNumberOfColumns(rq->hstmt);
/************************/
/* get dictionary */
/************************/
printf ("\nStructure for table: %s\n", DBTable);
printf ("Column Column Name Type\
Width Dec\n");
StatusReturned = SQL_SUCCESS;
ColPosition = 0;
vu->ColCount = 0;
TotalLength = 0;
do
{
/* point to column descriptor */
fi = &ColInfo[ColPosition];
StatusReturned = SQL_SUCCESS;
/* get data dictionary for
the current cursor ID */
StatusReturned =
dbDescribe( ColPosition+1,
fi, rq );
if ((StatusReturned != SQL_SUCCESS))
{
if (StatusReturned == ENDFILE)
{
#ifdef DEBUG
printf ("\n<QEDict> end of data dictionary\n");
#endif
break;
}
else
{
printf
("\n<QEdict> ERROR %d: \n",
StatusReturned);
printf
("\n getting data dictionary for %s\n",
SQLString);
exit (1);
}
};
vu->ColCount++; /* update column count */
/* re-calc cols length */
TotalLength +=
ColInfo[ColPosition].Length;
ColDataType = DecodeQEDataType(fi->DataType);
strcpy(DictColName,fi->ColName);
flen = strlen(fi->ColName);
for ( i=flen; i < DISPLAYLEN; i++ );
{
DictColName[i]='\x20';
}
DictColName[DISPLAYLEN]='\x0';
printf
("\n %3.0i %20.20s %12.12s %5d %2d",
vu->ColCount,
DictColName,
ColDataType,
fi->Length,
fi->Scale);
ColPosition++;
}
while (ColPosition < nColumns);
/* display summary */
printf ("\nTotal length: %d\n",TotalLength);
/****************/
/* close */
/****************/
StatusReturned = dbClose (si);
if ( StatusReturned )
{
printf
("\n<QEdict> ERROR %d: \n",
StatusReturned);
printf
("\n dbClose failed, disconnecting \
from: %s\n", dsrc->DBDriver);
exit (1);
}
}
/**************************************************
* FUNCTION NAME: GetArgs
*
* AUTHOR: Ken North
*
* SYNOPSIS:
*
* get table name and dictionary path from
* command line or user
*
***************************************************/
void GetArgs(int argc, char *arg1, char *arg2,
char *arg3, char *table, char *dict,
char *driver)
{
static char name [80];
if (argc > 1)
{
memset(name,'\x0',sizeof(name));
if (*arg1 == '?')
{
printf("\n Usage: QEDICT table \
dictionarypath\n");
printf ("\n QEDICT QE Lib\
data definitions\n");
printf(" (Requires QELIB DLL\
or QExxx.DLL)\n\n");
printf
(" To display a data dictionary screen, \
type:\n");
printf
(" QEDICT table dictpath driver| \
MORE\n\n");
printf
(" To list to a file, type:\n");
printf
(" QEDICT table dictpath driver> \
listfile\n\n\n");
exit(1);
}
strcpy (name, arg1);
strupr (name); /* translate to upper case */
strcpy(table,name) ; /* copy name to table */
memset(name,'\x0',sizeof(name));
}
if (argc > 2)
{
strcpy (name, arg2);
strupr (name); /* translate to upper case */
strcpy(dict,name) ;
memset(name,'\x0',sizeof(name));
}
if (argc > 3)
{
strcpy (name, arg3);
strupr (name); /* translate to upper case */
strcpy(driver,name) ;
}
if (argc < 2)
{
printf ("Table name ? ");
gets (name);
puts ("\n");
strupr (name); /* translate to upper case */
strcpy(table,name) ;
memset(name,'\x0',sizeof(name)); /* zero name */
printf ("Dictionary pathname ? ");
gets (name);
puts ("\n");
strupr (name);
strcpy(dict,name) ;
memset(name,'\x0',sizeof(name)); /* zero name */
printf ("Database driver ? ");
gets (name);
puts ("\n");
strupr (name);
strcpy(driver,name) ;
}
}